﻿Imports MeoTom.Core

Partial Public Class PageUpdate
    Inherits UI.Page
    Public a As New BLL.Pages()
    Public Util2 As New Utilities.Common()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Select Case Request.QueryString("MsgID")
            Case "2"
                lblError.Attributes.Remove("style")
                lblError.InnerHtml = "Post updated."
            Case "4"
                lblError.Attributes.Remove("style")
                lblError.InnerHtml = "Post updated."
            Case "6"
                lblError.Attributes.Remove("style")
                lblError.InnerHtml = "Post published."
            Case "7"
                lblError.Attributes.Remove("style")
                lblError.InnerHtml = "Post saved."
        End Select

        If Not IsPostBack Then
            Dim previousPage As String
            If Request.QueryString.ToString = vbNullString Then
                previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO"))
            Else
                previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO") & "?" & Request.QueryString.ToString())
            End If
            util2.ValidateLogin(previousPage)

            If Not Request.QueryString("ID") = vbNullString Then
                LoadArticle(Request.QueryString("ID"))
                ArticlePublishContent.InnerHtml = WriteScheduleDate()
                ArticleVisibleContent.InnerHtml = WriteVisibilityInf()
            End If
        End If
    End Sub

    Public Function WriteScheduleDate() As String
        Dim strOut As New StringBuilder()
        strOut.Append("<br /><select name=""mmmm"" id=""mmmm""><option value=""1"">Jan</option><option value=""2"">Feb</option><option value=""3"">Mar</option><option value=""4"">Apr</option><option value=""5"">May</option><option value=""6"">Jun</option><option value=""7"">Jul</option><option value=""8"">Aug</option><option value=""9"">Sep</option><option value=""10"">Oct</option><option value=""11"">Nov</option><option value=""12"">Dec</option></select> ")
        strOut.Append("<input type=""text"" style=""width:auto;"" id=""dd"" name=""dd"" value=""").Append(Day(a.ReleaseDate)).Append(""" size=""2"" maxlength=""2"" autocomplete=""off"" />")
        strOut.Append(", <input type=""text"" style=""width:auto;"" id=""yy"" name=""yy"" value=""").Append(Year(a.ReleaseDate)).Append(""" size=""4"" maxlength=""4"" tabindex=""4"" autocomplete=""off"" /> @ <input style=""width:auto;"" type=""text"" id=""hh"" name=""hh"" value=""").Append(Hour(a.ReleaseDate)).Append(""" size=""2"" maxlength=""2"" tabindex=""4"" autocomplete=""off"" /> : <input style=""width:auto;"" type=""text"" id=""mn"" name=""mn"" value=""").Append(Minute(a.ReleaseDate)).Append(""" size=""2"" maxlength=""2"" tabindex=""4"" autocomplete=""off"" /><input type=""hidden"" id=""ss"" name=""ss"" value=""").Append(Second(a.ReleaseDate)).Append(""" />")
        strOut.Append("<input type=""button"" value=""OK"" class=""button"" onclick=""javascript:Util.PublishConfirmAction();"" /> ")
        strOut.Append("<a onclick=""javascript:Util.CancelArticlePublish();"" title=""Cancel"" href=""javascript:void(0);"">Cancel</a><input type=""hidden"" id=""txtReleaseDate"" value=""").Append(a.ReleaseDate).Append(""" name=""txtReleaseDate"" />")
        strOut.Append("<script type=""text/javascript"" language=""javascript"">gmobj(""mmmm"").value = ").Append(Month(a.ReleaseDate)).Append(";</script>")
        Return strOut.ToString()
    End Function

    Public Function WriteVisibilityInf()
        Dim strCheck0 As String = " checked=""checked"" "
        Dim strCheck1 As String = " checked=""checked"" "
        Dim strCheck2 As String = " checked=""checked"" "
        If a.PostVisible = "0" Then
            strCheck1 = ""
            strCheck2 = ""
        ElseIf a.PostVisible = "1" Then
            strCheck0 = ""
            strCheck2 = ""
        ElseIf a.PostVisible = "2" Then
            strCheck0 = ""
            strCheck1 = ""
        End If
        Dim strOut As New StringBuilder()
        strOut.Append("<br /><label><input type=""radio""").Append(strCheck0).Append("name=""cboVisible"" id=""txtArticlePublic"" value=""0"" onclick=""javascript:Util.ArticlePublic();"" />Public</label><br />")
        strOut.Append("<label><input type=""radio""").Append(strCheck1).Append("name=""cboVisible"" id=""txtArticlePassword"" value=""1"" onclick=""javascript:Util.ArticlePassword();"" />Password protected</label><br />")
        strOut.Append("<span id=""frmArticlePwd"" style=""font-size:11px;display:none;""><label>Password<br /><input type=""text"" id=""txtArticlePwd"" name=""txtArticlePwd"" /></label><br /></span>")
        strOut.Append("<label><input type=""radio""").Append(strCheck2).Append("name=""cboVisible"" id=""txtArticlePrivate"" value=""2"" onclick=""javascript:Util.ArticlePrivate();"" />Private</label><br />")
        strOut.Append("<input type=""button"" class=""button"" value=""OK"" onclick=""javascript:Util.ConfirmAction('").Append(ArticleVisibleContent.ClientID).Append("');"" />&nbsp;&nbsp;<a href=""javascript:void(0);"" title=""Cancel"" onclick=""javascript:Util.CancelArticleVisible('").Append(ArticleVisibleContent.ClientID).Append("');"">Cancel</a>")
        strOut.Append("<input type=""hidden"" name=""txtPostVisible"" id=""txtPostVisible"" value=""").Append(a.PostVisible).Append(""" />")
        strOut.Append("<input type=""hidden"" name=""txtArticlePath"" id=""txtArticlePath"" value=""").Append(a.PagePath).Append(""" />")
        strOut.Append("<input type=""hidden"" name=""txtCreateDate"" id=""txtCreateDate"" value=""").Append(a.CreatedDate.ToString()).Append(""" />")
        Return strOut.ToString()
    End Function

    Public Sub LoadArticle(ByVal id As String)
        a.PageID = id
        a.GetByEdit()

        Select Case a.PostVisible
            Case "0"
                lblArticleVisiblity.Text = "Public"
            Case "1"
                lblArticleVisiblity.Text = "Password protected"
            Case "2"
                lblArticleVisiblity.Text = "Private"
        End Select

        If a.Status Then
            lblStatus.InnerHtml = "Status: <b>Published</b>"
            btnUpdate.Text = "Update Post"
            btnSave.Visible = False
        Else
            lblStatus.InnerHtml = "Status: <b>Draft</b>"
            btnUpdate.Style.Add("width", "30%")
        End If

        txtBody.Value = Server.HtmlDecode(a.Body)
        txtTitle.Text = a.PageTitle
        txtTitleNone.Value = a.PageTitleNone
        drpCommentEnabled.Checked = a.CommentsEnabled
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        If txtTitle.Text = vbNullString Then
            txtTitle.Text = "No Title"
        ElseIf txtTitle.Text.Length > 550 Then
            txtTitle.Text = Left(txtTitle.Text, 500)
        End If
        a.Status = False
        ArticleInfo()
        a.Create()
        a.GetAfterIns()
        a.WriteXMLByID(a.PageID, a.CreatedDate.Day, a.CreatedDate.Month, a.CreatedDate.Year, a.Body)

        Response.Redirect("/mt-inside/PageUpdate.aspx?ID=" & a.PageID & "&MsgID=7")
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
        Dim strArticleID As String = ""
        Dim d As DateTime
        If txtTitle.Text = vbNullString Then
            txtTitle.Text = "No Title"
        ElseIf txtTitle.Text.Length > 550 Then
            txtTitle.Text = Left(txtTitle.Text, 500)
        End If
        a.Status = True
        ArticleInfo()
        d = DateTime.Parse(Request.Form("txtCreateDate").ToString())
        strArticleID = a.PageID
        a.Update()
        If a.PagePath = vbNullString Then
            a.PagePath = "/Pages/" & a.PageID & "/" & a.PageTitleNone & ".aspx"
            a.UpdateArticlePath()
        End If
        a.WriteXMLByID(a.PageID, d.Day, d.Month, d.Year, a.Body)
        a.WriteTop10Last()
        Response.Redirect("/mt-inside/PageUpdate.aspx?ID=" & strArticleID & "&MsgID=2")
    End Sub

    Private Sub lnkDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDelete.Click
        a.PageID = Request.QueryString("ID")
        a.GetByEdit()

        util2.DeleteFile(Configs.PageFile & "ListFile\" & a.CreatedDate.Year.ToString() & "\" & a.CreatedDate.Month.ToString() & "\" & a.CreatedDate.Day.ToString() & "\" & a.PageID & ".xml")

        a.Delete()
        a.WriteTop10Last()
        Response.Redirect("/mt-inside/PageManager.aspx")
    End Sub

    Public Sub ArticleInfo()
        a.PageID = Request.QueryString("ID")
        a.PageTitle = util2.NoHTML(txtTitle.Text)
        a.Body = txtBody.Value
        a.CommentsEnabled = drpCommentEnabled.Checked
        a.PostVisible = Integer.Parse(Request.Form("txtPostVisible").ToString())
        a.PageTitleNone = txtTitleNone.Value
        a.PagePath = Request.Form.Get("txtArticlePath")

        If Not Request.Form.Get("txtArticlePwd") = vbNullString Then
            a.PagePassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Request.Form("txtArticlePwd").ToString(), "MD5")
        End If

        If Not Request.Form("txtReleaseDate").ToString() = vbNullString Then
            a.ReleaseDate = DateTime.Parse(Request.Form("txtReleaseDate").ToString())
        End If
    End Sub
End Class